import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/music_provider.dart';

class ProfilePage extends StatefulWidget {
  const ProfilePage({Key? key}) : super(key: key);

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> with SingleTickerProviderStateMixin {
  late TabController _tabController;
  int _selectedTabIndex = 0;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener(() {
      setState(() {
        _selectedTabIndex = _tabController.index;
      });
    });
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('img/木子米.jpeg'),
            fit: BoxFit.cover,
          ),
        ),
        child: SafeArea(
          child: SingleChildScrollView(
            child: Column(
              children: [
                // 顶部导航栏 - 简化版，避免重复
                _buildTopBar(),
                // 用户信息区
                _buildUserInfo(),
                // 功能按钮区
                _buildFunctionButtons(),
                // Tab导航栏
                _buildTabNavigation(),
                // Tab内容区
                _buildTabContent(),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 顶部导航栏
  Widget _buildTopBar() {
    return Container(
      width: double.infinity,
      height: 44,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: Row(
        children: [
          // 播放列表图标
          Container(
            width: 32,
            height: 32,
            alignment: Alignment.center,
            child: Icon(
              Icons.playlist_play,
              color: const Color(0xFF000000), // paint_7:190
              size: 22,
            ),
          ),
          
          const Spacer(),
          
          // +添加状态按钮
          Container(
            height: 32,
            padding: const EdgeInsets.symmetric(horizontal: 12),
            alignment: Alignment.center,
            child: Text(
              '+添加状态',
              style: TextStyle(
                color: const Color(0xFF3D3D3D), // paint_7:00
                fontSize: 14,
                fontFamily: 'Source Han Sans',
                fontWeight: FontWeight.bold,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          
          const Spacer(),
          
          // 右侧图标组
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 搜索图标
              Container(
                width: 32,
                height: 32,
                alignment: Alignment.center,
                child: Icon(
                  Icons.search,
                  color: const Color(0xFF000000), // paint_7:190
                  size: 22,
                ),
              ),
              
              const SizedBox(width: 12),
              
              // 添加图标
              Container(
                width: 32,
                height: 32,
                alignment: Alignment.center,
                child: Icon(
                  Icons.add_circle,
                  color: const Color(0xFFF73E3B), // paint_7:1628
                  size: 22,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 用户信息区
  Widget _buildUserInfo() {
    return Container(
      width: double.infinity,
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
      padding: const EdgeInsets.symmetric(vertical: 20),
      decoration: BoxDecoration(
        color: Colors.white.withOpacity(0.9),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        children: [
          // 头像
          Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(40),
              image: const DecorationImage(
                image: AssetImage('img/Anno.jpeg'), // 使用本地头像图片
                fit: BoxFit.cover,
              ),
            ),
          ),
          
          const SizedBox(height: 16),
          
          // 用户名
          Text(
            'Anna_Yannami',
            style: TextStyle(
              color: const Color(0xFF3D3D3D), // paint_7:00
              fontSize: 14,
              fontFamily: 'Source Han Sans',
              fontWeight: FontWeight.w900,
            ),
          ),
          
          const SizedBox(height: 5),
          
          // 地理位置
          Text(
            '浙江  金华',
            style: TextStyle(
              color: const Color(0xFF3D3D3D), // paint_7:00
              fontSize: 12,
              fontFamily: 'Source Han Sans',
              fontWeight: FontWeight.bold,
            ),
          ),
          
          const SizedBox(height: 16),
          
          // 统计信息
          Text(
            '12 关注   2粉丝   Lv.8   988小时',
            style: TextStyle(
              color: const Color(0xFF3D3D3D), // paint_7:00
              fontSize: 14,
              fontFamily: 'Source Han Sans',
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  // 功能按钮区
  Widget _buildFunctionButtons() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          _buildFunctionButton(Icons.access_time, '最近', const Color(0xFFA6A6A6)),
          _buildFunctionButton(Icons.file_download, '本地', const Color(0xFFA6A6A6)),
          _buildFunctionButton(Icons.cloud_upload, '网盘', const Color(0xFFA6A6A6)),
          _buildFunctionButton(Icons.style, '装扮', const Color(0xFFA6A6A6)),
        ],
      ),
    );
  }

  Widget _buildFunctionButton(IconData icon, String text, Color iconColor) {
    return Container(
      width: 80,
      height: 35,
      decoration: BoxDecoration(
        color: const Color.fromRGBO(216, 216, 216, 0.8), // paint_7:3765
        borderRadius: BorderRadius.circular(10),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(icon, color: iconColor, size: 16),
          const SizedBox(width: 6),
          Text(
            text,
            style: TextStyle(
              color: const Color(0xFF3D3D3D), // paint_7:00
              fontSize: 14,
              fontFamily: 'Source Han Sans',
              fontWeight: FontWeight.bold,
            ),
          ),
        ],
      ),
    );
  }

  // Tab导航栏
  Widget _buildTabNavigation() {
    return Container(
      width: double.infinity,
      height: 55,
      margin: const EdgeInsets.symmetric(horizontal: 6, vertical: 10),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(216, 216, 216, 0.8), // paint_7:3765
        borderRadius: BorderRadius.circular(10),
      ),
      child: Row(
        children: [
          _buildTabItem('音乐', 0, const Color.fromRGBO(255, 255, 255, 0.8)), // paint_7:4191
          _buildTabItem('博客', 1, const Color.fromRGBO(255, 255, 255, 0.8)), // paint_7:4191
          _buildTabItem('笔记', 2, const Color.fromRGBO(255, 255, 255, 0.8)), // paint_7:4191
        ],
      ),
    );
  }

  Widget _buildTabItem(String text, int index, Color textColor) {
    final isSelected = _selectedTabIndex == index;
    return Expanded(
      child: GestureDetector(
        onTap: () {
          _tabController.animateTo(index);
        },
        child: Container(
          height: 35,
          margin: const EdgeInsets.all(10),
          alignment: Alignment.center,
          child: Text(
            text,
            style: TextStyle(
              color: isSelected ? const Color(0xFFF73E3B) : textColor,
              fontSize: 22,
              fontFamily: 'Source Han Sans',
              fontWeight: isSelected ? FontWeight.w900 : FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }

  // Tab内容区
  Widget _buildTabContent() {
    return Container(
      width: double.infinity,
      constraints: const BoxConstraints(minHeight: 300),
      margin: const EdgeInsets.symmetric(horizontal: 6),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: const Color.fromRGBO(216, 216, 216, 0.8), // paint_7:3765
        borderRadius: BorderRadius.circular(10),
      ),
      child: Column(
        children: [
          // 子导航
          _buildSubNavigation(),
          const SizedBox(height: 16),
          // 内容
          _selectedTabIndex == 0 ? _buildMusicContent() : _buildOtherContent(),
        ],
      ),
    );
  }

  // 子导航
  Widget _buildSubNavigation() {
    return Row(
      children: [
        Text(
          '近期',
          style: TextStyle(
            color: const Color(0xFFFFFFFF), // paint_7:3
            fontSize: 14,
            fontFamily: 'Source Han Sans',
            fontWeight: FontWeight.normal,
          ),
        ),
        const SizedBox(width: 30),
        Text(
          '创建',
          style: TextStyle(
            color: const Color(0xFFFFFFFF), // paint_7:3
            fontSize: 14,
            fontFamily: 'Source Han Sans',
            fontWeight: FontWeight.normal,
          ),
        ),
        const SizedBox(width: 30),
        Text(
          '收藏',
          style: TextStyle(
            color: const Color(0xFFFFFFFF), // paint_7:3
            fontSize: 14,
            fontFamily: 'Source Han Sans',
            fontWeight: FontWeight.normal,
          ),
        ),
        const Spacer(),
        Icon(
          Icons.more_vert,
          color: const Color(0xFFFFFFFF), // paint_7:389
          size: 20,
        ),
      ],
    );
  }

  // 音乐内容
  Widget _buildMusicContent() {
    final coverImages = _getPlaylistCoverImages();
    return Column(
      children: [
        _buildPlaylistItem(
          coverImages[0],
          '我喜欢的音乐',
          '437首·9654次播放',
          true,
        ),
        const SizedBox(height: 16),
        _buildPlaylistItem(
          coverImages[1],
          '柚子社 op/ed',
          '歌单·437首·龙岩玻璃',
          false,
        ),
        const SizedBox(height: 16),
        _buildPlaylistItem(
          coverImages[2],
          '我喜欢的音乐',
          '歌单·437首·龙哥哥今天又鸽了',
          false,
        ),
        const SizedBox(height: 16),
        _buildPlaylistItem(
          coverImages[3],
          '动漫/神曲音乐集',
          '歌单·130首·GILASFM',
          false,
        ),
      ],
    );
  }

  Widget _buildPlaylistItem(String imageUrl, String title, String subtitle, bool showHeart) {
    return Container(
      width: double.infinity,
      height: 65,
      child: Row(
        children: [
          // 封面图片
          Container(
            width: 50,
            height: 50,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(4),
              image: DecorationImage(
                image: imageUrl.startsWith('http') 
                    ? NetworkImage(imageUrl) as ImageProvider
                    : AssetImage(imageUrl),
                fit: BoxFit.cover,
              ),
            ),
            child: showHeart ? 
              Center(
                child: Icon(
                  Icons.favorite,
                  color: const Color(0xFFFFFFFF), // paint_7:389
                  size: 20,
                ),
              ) : null,
          ),
          
          const SizedBox(width: 12),
          
          // 文字信息
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    color: const Color(0xFFFFFFFF), // paint_7:3
                    fontSize: 14,
                    fontFamily: 'Source Han Sans',
                    fontWeight: FontWeight.w900,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  subtitle,
                  style: TextStyle(
                    color: const Color.fromRGBO(255, 255, 255, 0.8), // paint_7:4191
                    fontSize: 10,
                    fontFamily: 'Source Han Sans',
                    fontWeight: FontWeight.w900,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  // 其他内容（博客、笔记）
  Widget _buildOtherContent() {
    return Container(
      height: 200,
      alignment: Alignment.center,
      child: Text(
        _selectedTabIndex == 1 ? '博客内容' : '笔记内容',
        style: TextStyle(
          color: const Color(0xFFFFFFFF), // paint_7:3
          fontSize: 16,
          fontFamily: 'Source Han Sans',
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  List<String> _getPlaylistCoverImages() {
    return [
      'img/Anno.jpeg',       // 使用本地图片
      'img/木子米.jpeg',      // 使用本地图片
      'img/音乐播放页-木子米.jpeg', // 使用本地图片
      'img/推荐页/每日推荐.jpeg',  // 使用本地图片
    ];
  }
} 